package com.cloudkinto.controller.storage;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.storage.*;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.storage.StorageLocationService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.storage.CellAvailableVo;
import com.cloudkinto.vo.storage.StorageLocationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库位表 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-11-12
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "库位表模块")
@RequestMapping("/api/admin/storage/location")
public class StorageLocationController extends BaseController {
    private final StorageLocationService storageLocationService;

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "库位表新增", notes = "库位表新增")
    public SingleResult add(@RequestBody @Validated StorageLocationAddBo req) {
        storageLocationService.add(req, getTenantId());
        return SingleResult.success("新增成功");
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "库位表修改", notes = "库位表修改")
    public SingleResult update(@RequestBody @Validated StorageLocationUpdateBo req) {
        Long userId = getCurrentUserId();
        storageLocationService.update(req, userId);
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @DeleteMapping("/batchDelete")
    @ApiOperation(value = "库位表根据id删除", notes = "库位表根据id删除")
    public SingleResult<CommonBatchOperationVo> delete(@RequestParam(value = "ids") Long[] ids) {
        return SingleResult.success("删除成功", storageLocationService.delete(Arrays.asList(ids)));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "库位表根据id查看详情", notes = "库位表根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", storageLocationService.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "库位表查询初始化", notes = "库位表查询初始化")
    public SingleResult<Map<String, Object>> pageInit() {
        return storageLocationService.pageInit(getTenantId(), getWarehouseId());
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "库位表查询（分页）", notes = "库位表查询（分页）")
    public PageResult<StorageLocationVo> listPage(@RequestBody StorageLocationQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setWhId(getWarehouseId());
        return storageLocationService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("importAdd")
    @ApiOperation(value = "导入库位", notes = "导入库位")
    public SingleResult importAdd(StorageImportBo bo) {
        return SingleResult.success(storageLocationService.importAdd(bo, getTenantId()));
    }

    @UserSessionRequired
    @PostMapping("importUpdate")
    @ApiOperation(value = "导入更新库位", notes = "导入更新库位")
    public SingleResult importUpdate(StorageImportBo bo) {
        return SingleResult.success(storageLocationService.importUpdate(bo, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/export")
    @ApiOperation(value = "导出库位", notes = "导出库位")
    public void export(@RequestParam(value = "ids", required = false) List<Long> ids, HttpServletResponse response) throws IOException {
        storageLocationService.export(ids, getTenantId(), response);
    }

    @UserSessionRequired
    @PostMapping("/getCellAvailableByBox")
    @ApiOperation(value = "根据箱号获取库位可用列表", notes = "根据箱号获取库位可用列表")
    public SingleResult<List<CellAvailableVo>> getCellAvailableByBox(@RequestBody CellAvailableByBoxQueryBo bo) {
        bo.setTenantId(getTenantId());
        return SingleResult.success(storageLocationService.getCellAvailableByBox(bo));
    }

    @UserSessionRequired
    @PostMapping("/getCellAvailableBySku")
    @ApiOperation(value = "根据sku获取库位可用列表", notes = "根据sku获取库位可用列表")
    public SingleResult<List<CellAvailableVo>> getCellAvailableBySku(@RequestBody CellAvailableBySkuQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setWhId(getWarehouseId());
        return SingleResult.success(storageLocationService.getCellAvailableByProductId(bo));
    }
}

